package com.atguigu.jxc.util;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 根据菜单数据构建菜单树的工具类
 * 不是所有的都能用的工具类
 */
public class TreeHelper<M extends List<T>, T> {


    public M  buildTree(M list) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {

        List<T> trees = new ArrayList<>();
        for(T sys : list){
            //反射得到方法
            Method method = sys.getClass().getMethod("getpId");
            //执行方法得到返回值
            Object o = sys.getClass().newInstance();
            Long meths=(Long) method.invoke(sys);
            if (meths.longValue()==0){
                trees.add(findChildren(sys,list));
            }
        }
        return (M) trees;
    }
    /*   for (SysMenu it : treeNodes) {
      if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
          if (sysMenu.getChildren() == null) {
              sysMenu.setChildren(new ArrayList<>());
          }
          sysMenu.getChildren().add(findChildren(it,treeNodes));
      }
  }*/
    public  T  findChildren(T sys,M list) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Method setChildren = sys.getClass().getMethod("setChildren", List.class);
        setChildren.invoke(sys,new ArrayList<T>());
        for(T it:list){
            Method method = sys.getClass().getMethod("getGoodsTypeId");
            Long  invoke1 = (Long) method.invoke(sys);
            //第二个
            Method getParentId = it.getClass().getMethod("getpId");
            Long invoke = (Long) getParentId.invoke(it);
            if(invoke1.longValue() == invoke.longValue()){
                Method getId = sys.getClass().getMethod("getGoodsTypeId");
                Long invoke2 = (Long) getId.invoke(sys);
                if (invoke2==null) {
                    sys.getClass().getMethod("setChildren",ArrayList.class).invoke(sys,new ArrayList<>());
                }
                List<T>   sysList= (List<T>) sys.getClass().getMethod("getChildren").invoke(sys);
                sysList.add(findChildren(it,list));
            }
        }
        return sys;
    }

    /**
     * 使用递归方法建菜单
     * @param sysMenuList
     * @return
     */

 /*   public static List<SysMenu> buildTree(List<SysMenu> sysMenuList) {
        List<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getParentId().longValue() == 0) {
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    *//**
     * 递归查找子节点
     * @param treeNodes
     * @return
     *//*
    public static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> treeNodes) {
        sysMenu.setChildren(new ArrayList<SysMenu>());

        for (SysMenu it : treeNodes) {
            if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
                if (sysMenu.getChildren() == null) {
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysMenu;
    }*/
}